/*
package com.zyh.websocket.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zyh.common.core.domain.entity.SysUser;
import com.zyh.common.utils.SecurityUtils;
import com.zyh.counselor.service.ICounselorService;
import com.zyh.system.service.ISysUserService;
import com.zyh.websocket.constant.MessageConstant;
import com.zyh.websocket.controller.WebSocketController;
import com.zyh.websocket.entity.Message;
import com.zyh.websocket.service.MessageBaseService;
import com.zyh.websocket.service.chat.MessageService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

*/
/**
 * @author ZYH
 * @version 1.0
 * @date 2025-03-26
 *//*

@Slf4j
@Component
@AllArgsConstructor
public class MessagePushUtil {
    private static SimpMessagingTemplate messagingTemplate;
    private static MessageService messageService;
    private static ISysUserService userService;
    private final ICounselorService counselorService;

    @Autowired
    public void setMessagingTemplate(SimpMessagingTemplate messagingTemplate) {
        MessagePushUtil.messagingTemplate = messagingTemplate;
    }

    @Autowired
    public void setMessageService(MessageService messageService) {
        MessagePushUtil.messageService = messageService;
    }

    @Autowired
    public void setWebSocketMessageController(WebSocketController webSocketMessageController) {
        MessagePushUtil.webSocketMessageController = webSocketMessageController;
    }

    @Autowired
    public void setUserService(ISysUserService userService) {
        MessagePushUtil.userService = userService;
    }

    */
/**
     * 发送系统公告（管理员操作）
     *
     * @param adminId 管理员ID（用于鉴权）
     * @param content 公告内容
     *//*

    public static void sendAnnouncement(Long adminId, String content, String linkUrl) {
        boolean flag = SecurityUtils.isAdmin(adminId);
        if (!flag) {
            return;
        }
        Message msg = Message.builder()
                .senderId(MessageConstant.ADMIN_ID)
                .receiverId(MessageConstant.ADMIN_ID)
                .type(MessageConstant.SYSTEM_MSG)
                .subType(MessageConstant.NOTIFY_ALL)
                .content(buildJsonContent("系统公告", content, linkUrl))
                .sendTime(new Date())
                .build();

        saveAndPush(msg);
    }

    */
/**
     * 发送审核请求（用户→管理员）
     *
     * @param userId 申请用户ID
     *//*

    public static void sendApprovalRequest(Long userId, String content, Long businessId) {
        Message msg = Message.builder()
                .senderId(userId)
                .receiverId(MessageConstant.ADMIN_ID)
                .type(MessageConstant.SYSTEM_MSG)
                .subType(MessageConstant.ADMIN_APPROVAL_REQUEST)
                .content(content)
                .relatedId(businessId)
                .sendTime(new Date())
                .build();

        saveAndPush(msg);
    }

    */
/**
     * 发送审核结果（管理员→用户）
     *
     * @param adminId 处理管理员
     * @param userId  目标用户
     *//*

    public static void sendApprovalResult(Long adminId, Long userId,
                                          String content, Long businessId) {
        Message msg = Message.builder()
                .senderId(adminId)
                .receiverId(userId)
                .type(MessageConstant.SYSTEM_MSG)
                .subType(MessageConstant.ADMIN_APPROVAL_RESULT)
                .content(content)
                .relatedId(businessId)
                .sendTime(new Date())
                .build();

        saveAndPush(msg);
    }

    */
/**
     * 发送私聊消息
     *
     * @param senderId   发送方
     * @param receiverId 接收方
     * @param content    内容
     * @param subType    会话类型（counselor_chat/user_chat）
     *//*

    public static void sendPrivateMessage(Long senderId, Long receiverId,
                                          String content, String subType) {
        Message msg = Message.builder()
                .senderId(senderId)
                .receiverId(receiverId)
                .type(MessageConstant.PRIVATE_MSG)
                .subType(MessageConstant.CHAT)
                .content(content)
                .sendTime(new Date())
                .build();

        String destination = counselorService.isCounselor(receiverId) ?
                String.format(MessageConstant.COUNSELOR_QUEUE, receiverId) :
                String.format(MessageConstant.USER_QUEUE, receiverId);

        messageService.save(msg);
        messagingTemplate.convertAndSend(destination, msg);
    }

    // 统一保存并推送
    private static void saveAndPush(Message msg) {
        messageService.save(msg);

        if (MessageConstant.NOTIFY_ALL.equals(msg.getSubType())) {
            messagingTemplate.convertAndSend(MessageConstant.SYSTEM_TOPIC, msg);
        } else if (msg.getReceiverId() == MessageConstant.ADMIN_ID) {
            handleBroadcastToAdmin(msg); // 新增管理员广播逻辑
        } else {
            String destination = determineDestination(msg);
            messagingTemplate.convertAndSend(destination, msg);
        }
    }

    private static String determineDestination(Message msg) {
        if (MessageConstant.PRIVATE_MSG.equals(msg.getType())) {
            return messageBaseService.isCounselor(msg.getReceiverId()) ?
                    String.format(MessageConstant.COUNSELOR_QUEUE, msg.getReceiverId()) :
                    String.format(MessageConstant.USER_QUEUE, msg.getReceiverId());
        }
        return String.format(MessageConstant.USER_QUEUE, msg.getReceiverId());
    }

    private static void handleBroadcastToAdmin(Message msg) {
        SysUser admin = userService.getById(MessageConstant.ADMIN_ID);
        String destination = String.format(MessageConstant.USER_QUEUE, admin.getUserId());
        messagingTemplate.convertAndSend(destination, msg);
    }

    private static String buildJsonContent(String title, String detail, String link) {
        try {
            Map<String, Object> contentMap = new HashMap<>();
            contentMap.put("title", title);
            contentMap.put("detail", detail);
            contentMap.put("link", link);
            contentMap.put("timestamp", new Date().getTime());
            return new ObjectMapper().writeValueAsString(contentMap);
        } catch (JsonProcessingException e) {
            log.error("内容序列化失败", e);
            return "{\"error\":\"内容生成异常\"}";
        }
    }
}
*/
